this.WbstChart = this.WbstChart || {};
(function(){
	var map = function(containerId)
	{
		this._containerId = containerId;
		this.creationMapChart();

		this.EventDispatcher = $({});
	}

	var p = map.prototype;

	p.init = function()
	{

	}

	p.setConfig = function(value)
	{
		this._config = value;
	}

	p.setFillData = function(value)
	{
		this._fillData = value;
        this.creationFill();
	}
	p.setLineData = function(value)
	{
		this._lineData = value;
		this.creationLine();
	}
	p.setDotData = function(value)
	{
		this._dotData = value;
		this.creationDot();
	}

	p.creationMapChart = function()
	{
        this._renderer = PIXI.autoDetectRenderer(window.innerWidth, window.innerHeight, {transparent: true});
        document.getElementById(this._containerId).appendChild(this._renderer.view);
        this._stage = new PIXI.Stage();

        this._mapChart = new WbstChart.MapChart(this._stage, this._renderer);
        // this._styleCoordinator = new WbstChart.AreaFillStyleCoordinator();
        this._mapChart.styleCoordinator(this._styleCoordinator);

        var current = this;
        this._mapChart.EventDispatcher.on('MapChartItemMouseOver', function(event, item)
        {
            current.EventDispatcher.trigger('MapChartItemMouseOver', {event: event, data: current._dataMap[item._zh_name]});
        });

        this._mapChart.EventDispatcher.on('removeElements', function() {
            current.removeElements();
        });
        this._mapChart.EventDispatcher.on('addElements', function() {
            current.addElements();
        });
	}

	p.resize = function(size)
	{
		if (size) this._size = size;
        this._renderer.resize(this._size.width, this._size.height);
        this._mapChart.resize(this._size);

        var current = this;
        // 设置500毫秒的延时，否则地图还没绘制完成，获取的XY坐标不准确。
        setTimeout(function(){

			if (current._curves)
	        {
	            for (var i = 0; i < current._curves.length; i++)
	            {
	                var curve = current._curves[i];
	                var source = curve._source;
	                var target = curve._target;

	                var point = current._mapChart.getPoint(source.x, source.y);
                    point.x += 50;
	                curve.p0(point);
	                point = current._mapChart.getPoint(target.x, target.y);
                    point.x += 50;
	                curve.p2(point);

	                curve.init();
	                curve.render();
	            }
	        }

	        if (current._dots)
	        {
	            for (var i = 0; i < current._dots.length; i++)
	            {
	                var ring = current._dots[i];
	                var point = current._mapChart.getPoint(ring._position.x, ring._position.y);
                    point.x += 50;
	                ring.p0(point);

	                ring.init();
	                ring.render();
	            }
	        }
        }, 500);

        this._renderer.render(this._stage);
	}

    p.addElements = function()
    {
        var current = this;
        // 设置500毫秒的延时，否则地图还没绘制完成，获取的XY坐标不准确。
        setTimeout(function(){

            if (current._curves)
            {
                for (var i = 0; i < current._curves.length; i++)
                {
                    var curve = current._curves[i];
                    current._stage.addChild(curve._lineGraphics);
                    current._stage.addChild(curve._fireballGraphics);

                    var source = curve._source;
                    var target = curve._target;

                    var point = current._mapChart.getPoint(source.x, source.y);
                    point.x += 50;
                    curve.p0(point);
                    point = current._mapChart.getPoint(target.x, target.y);
                    point.x += 50;
                    curve.p2(point);

                    curve.init();
                    curve.render();
                }
            }

            if (current._dots)
            {
                for (var i = 0; i < current._dots.length; i++)
                {
                    var dot = current._dots[i];
                    if (dot._data[current._config.dotType] == 0)
                    {
                        current._stage.addChild(dot._graphics1);
                        current._stage.addChild(dot._graphics2);
                        current._stage.addChild(dot._graphics3);
                    }
                    else if (dot._data[current._config.dotType] == 1)
                    {
                        current._stage.addChild(dot._graphics1);
                        current._stage.addChild(dot._graphics2);
                    }
                    else
                    {
                        current._stage.addChild(dot._graphics);
                    }

                    var point = current._mapChart.getPoint(dot._position.x, dot._position.y);
                    point.x += 50;
                    dot.p0(point);

                    dot.init();
                    dot.render();
                }
            }
        }, 500);

        this._renderer.render(this._stage);
    }
    p.removeElements = function()
    {
        var current = this;
        if (current._curves)
        {
            for (var i = 0; i < current._curves.length; i++)
            {
                var curve = current._curves[i];
                this._stage.removeChild(curve._lineGraphics);
                this._stage.removeChild(curve._fireballGraphics);
            }
        }

        if (current._dots)
        {
            for (var i = 0; i < current._dots.length; i++)
            {
                var dot = current._dots[i];
                if (dot._data[this._config.dotType] == 0)
                {
                    this._stage.removeChild(dot._graphics1);
                    this._stage.removeChild(dot._graphics2);
                    this._stage.removeChild(dot._graphics3);
                }
                else if (dot._data[this._config.dotType] == 1)
                {
                    this._stage.removeChild(dot._graphics1);
                    this._stage.removeChild(dot._graphics2);
                }
                else
                {
                    this._stage.removeChild(dot._graphics);
                }
            }
        }
    }

	p.creationLine = function()
    {
        if (this._config == null || this._lineData == null) return;

        if (this._curves)
        {
            for (var i = 0; i < this._curves.length; i++)
            {
                var curve = this._curves[i];
                this._stage.removeChildAt(this._stage.getChildIndex(curve._lineGraphics));
                this._stage.removeChildAt(this._stage.getChildIndex(curve._fireballGraphics));
            }
        }

        this._curves = [];

        for (var i = 0; i < this._lineData.length; i++)
        {
            var data = this._lineData[i];

            var source = {x: data[this._config.sourceLng], y: data[this._config.sourceLat]};
            var target = {x: data[this._config.targetLng], y: data[this._config.targetLat]};

            var curve = new AnimatedObject.AnimatedCurve('curve');
            this._stage.addChild(curve._lineGraphics);
            this._stage.addChild(curve._fireballGraphics);
            curve._source = source;
            curve._target = target;
            curve._data = data;
            curve.numFireballs(2);

            this._curves.push(curve);
        }
        this.resize();
    }
    p.creationDot = function()
    {
        if (this._config == null || this._dotData == null) return;

        if (this._dots)
        {
            for (var i = 0; i < this._dots.length; i++)
            {
                var ring = this._dots[i];
                this._stage.removeChildAt(this._stage.getChildIndex(ring._shape));
            }
        }

        this._dots = [];

        var current = this;
        this._dotData.sort(function(a, b)
        {
            return a[current._config.dotValue] - b[current._config.dotValue];
        });
        var maxValue = this._dotData[this._dotData.length - 1][this._config.dotValue];
        var minValue = this._dotData[0][this._config.dotValue];
        var value = maxValue - minValue;

        for (var i = 0; i < this._dotData.length; i++)
        {
            var data = this._dotData[i];

            var position = {x: data[this._config.dotLng], y: data[this._config.dotLat]};
            var radius = maxValue == minValue ? 15 : 15 + (data[this._config.dotValue] - minValue) / value * (30 - 15);

            if (data[this._config.dotType] == 0)
            {
                var dot = new AnimatedObject.AnimatedRing('dot');
                this._stage.addChild(dot._graphics1);
                this._stage.addChild(dot._graphics2);
                this._stage.addChild(dot._graphics3);
            }
            else if (data[this._config.dotType] == 1)
            {
                dot = new AnimatedObject.AnimatedRect('dot');
                radius = maxValue == minValue ? 8 : 8 + (data[this._config.dotValue] - minValue) / value * (15 - 8);
                this._stage.addChild(dot._graphics1);
                this._stage.addChild(dot._graphics2);
            }
            else
            {
                dot = new AnimatedObject.AnimatedArrow('dot');
                radius = maxValue == minValue ? 30 : 30 + (data[this._config.dotValue] - minValue) / value * (40 - 30);
                this._stage.addChild(dot._graphics);
            }
            dot._position = position;
            dot._data = data;
            dot.radius(radius);

            this._dots.push(dot);
        }
        this.resize();
    }
    p.creationFill = function()
    {
        if (this._config == null || this._fillData == null) return;

        if (this._colorRank == null) this._colorRank = new WbstChart.ColorRank();
        this._stage.addChild(this._colorRank._graphics);

        this._dataMap = {};
        var listCount = [];

        for (var i = 0; i < this._fillData.length; i++)
        {
            var data = this._fillData[i];
            var num = Number(data[this._config.areaValue]);
            if (isNaN(num)) continue;

            this._dataMap[data[this._config.areaField]] = data;

            if (listCount.indexOf(num) == -1) listCount.push(num);
        }

        var current = this;
        listCount.sort(function(a, b)
        {
            return a - b;
        });
        this._colorRank.maxValue(listCount[listCount.length - 1]);
        this._colorRank.minValue(listCount[0]);

        // this._styleCoordinator.styleFunction(function(g, level, info, type)
        // {
        //     var color;
        //     if (type == 'mouseout')
        //     {
        //         var item = current._dataMap[info.name];
        //         if (item)
        //         {
        //             var num = item[current._config.areaValue];
        //             color = current._colorRank.getColor(num);
        //         }
        //         else color = 0x005296;

        //         g.beginFill(color);
        //     }
        //     else
        //     {
        //         g.beginFill(0xffff00);
        //     }
        //     g.lineStyle(1, 0x16a2b5);
        // });

        this.resize();
    }

    // 渲染。
    p.render = function()
    {
        var current = this;
        setInterval(function()
        {
            if (current._curves)
            {
                for (var i = 0; i < current._curves.length; i++)
                {
                    var curve = current._curves[i];
                    curve.render();
                }
            }

            if (current._dots)
            {
                for (var i = 0; i < current._dots.length; i++)
                {
                    var ring = current._dots[i];
                    ring.render();
                }
            }

            current._renderer.render(current._stage);
        }, 50);
    }

	WbstChart.Map = map;
})();

/**
 * 区域填充样式协调器。
 */
(function(){
    var areaFillStyleCoordinator = function()
    {

    }

    var p = areaFillStyleCoordinator.prototype;

    p.styleFunction = function(value)
    {
        this._styleFunction = value;
    }

    p.styleShape = function(g, level, info, type)
    {
        if (this._styleFunction)
        {
            this._styleFunction(g, level, info, type);
        }
        else
        {
            if (type == 'mouseout')
            {
                g.beginFill(0x005296).lineStyle(1, 0x16a2b5);
            }
            else if (type == 'mouseover')
            {
                g.beginFill(0xff0000).lineStyle(1, 0x16a2b5);
            }
        }
    }

    WbstChart.AreaFillStyleCoordinator = areaFillStyleCoordinator;
})();

/**
 * 区域填充图例。
 */
(function(){
    var colorRank = function()
    {
        this._r1 = 2;
        this._g1 = 29;
        this._b1 = 54;   // 颜色较深
        this._r2 = 0;
        this._g2 = 202;
        this._b2 = 252;   // 颜色较浅

        this._graphics = new PIXI.Graphics();
    }

    var p = colorRank.prototype;

    p.maxValue = function(value)
    {
        this._maxValue = value;
    }
    p.minValue = function(value)
    {
        this._minValue = value;
    }

    p.getColor = function(value)
    {
        var a = value - this._minValue;
        var b = this._maxValue - this._minValue;

        var r = parseInt(this._r2 + a / b * (this._r1 - this._r2));
        r = r > 9 ? r : '0' + r;
        var g = parseInt(this._g2 + a / b * (this._g1 - this._g2));
        g = g > 9 ? g : '0' + g;
        var b = parseInt(this._b2 + a / b * (this._b1 - this._b2));
        b = b > 9 ? b : '0' + b;
        return '0x' + r.toString(16) + g.toString(16) + b.toString(16);
    }

    WbstChart.ColorRank = colorRank;
})();